package com.lxd.api.filter;

import com.lxd.common.model.StandardSubmit;
import org.checkerframework.checker.units.qual.A;
import org.checkerframework.checker.units.qual.C;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * @Author Dongdong
 * @注释 策略模式的上下文
 */
@Component
//实现动态刷新 注解
@RefreshScope
public class CheckFilterContext {

    // 基于IOC 将CheckFilter 拿到所有的对象 放入map中
    @Autowired
    private Map<String,CheckFilter> checkFilterMap;
    // 校验链，从nacos读取
    @Value("${filters:apikey,ip,sign}")
    private String filters;

    /**
     * 校验方法，按filter顺序对对象进行校验
     * @param submit 被校验对象
     */
    public  void  check(StandardSubmit submit){
        //将 filters 转化成数组
        String[] filtersArray = filters.split(",");
        //遍历校验链，实现不同的检验
        for (String filter: filtersArray) {
            CheckFilter checkFilter = checkFilterMap.get(filter);
            checkFilter.check(submit);
        }
    }


}
